/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.core.service.impl;

import static com.google.gwt.thirdparty.guava.common.base.Preconditions.checkArgument;

import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.smouquet.ecovoiturage.core.dao.UtilisateurDAO;
import com.smouquet.ecovoiturage.core.domain.Utilisateur;
import com.smouquet.ecovoiturage.core.service.UtilisateurService;

/**
 *
 * @author sebastien.mouquet
 */
public class UtilisateurServiceImpl implements UtilisateurService
{
    @Inject
    private UtilisateurDAO dao;

    
    @Override
    public Utilisateur connexion(String login, String password)
    {
        checkArgument(login != null && !login.isEmpty(), "login is required");
        checkArgument(password != null && !password.isEmpty(), "password is required");
        
        
        Utilisateur u = dao.findByEmail(login);
        
        if(u == null || !password.equals(u.getMdp())) {
            return null;
        }
        
        return u;
    }

    @Override
    @Transactional
    public void add(Utilisateur utilisateur)
    {
        checkArgument(utilisateur != null, "utilisateur is required");
        
        dao.persist(utilisateur);
    }

    @Override
    @Transactional
    public Utilisateur updateUtilisateur(Utilisateur utilisateur)
    {
        checkArgument(utilisateur != null, "utilisateur is required");
        
        return dao.merge(utilisateur);
    }
    
    
}
